package com.liugh.controller.zhongche;

import com.baomidou.mybatisplus.plugins.Page;
import com.liugh.base.BusinessException;
import com.liugh.base.PublicResultConstant;
import com.liugh.config.ResponseModel;
import com.liugh.controller.BaseRestController;
import com.liugh.entity.standard.Announcement;
import com.liugh.model.DownloadModel;
import com.liugh.service.IUserService;
import com.liugh.service.zhongche.IAnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**标准化公告 .
 * Created by Fatao on 2018/10/31 .
 */
@Api(description = "标准化公告")
@RestController
@RequestMapping("/announcement")
@Slf4j
public class AnnouncementController extends BaseRestController {


    @Autowired
    private IAnnouncementService announcementService;


    @Autowired
    private IUserService userService;


    /**
     * 新增公告 .
     * @param announcement .
     * @return .
     */
    @ApiOperation("添加公告")
    @PostMapping("/add")
    public ResponseModel<Announcement> addAnnouncement(@RequestBody Announcement announcement) throws BusinessException {
        Announcement temp = announcementService.addAnnouncement(announcement);
        if (temp == null) {
            throw new BusinessException(PublicResultConstant.ERROR);
        }
        return fillSuccessData(temp);
    }



    /**
     * 删除公告 .
     * @param id .
     * @return .
     */
    @ApiOperation("删除公告")
    @DeleteMapping("/delete")
    public ResponseModel<String> addAnnouncement(@RequestParam("id")int id) throws BusinessException {
        int result = announcementService.delAnnouncementById(id);
        return result > 0 ? fillSuccessData(PublicResultConstant.SUCCEED) : fillFailData(PublicResultConstant.ERROR);
    }



    /**
     * 更新公告 .
     * @param announcement .
     * @return .
     */
    @ApiOperation("更新公告")
    @PostMapping("/update")
    public ResponseModel<Announcement> updateAnnouncement(@RequestBody Announcement announcement) throws BusinessException {
        announcement = announcementService.updateAnnouncementById(announcement);
        if (announcement == null) {
            throw new BusinessException(PublicResultConstant.ERROR);
        }
        return fillSuccessData(announcement);
    }



    /**
     * 获取公告 .
     * @param id .
     * @return .
     */
    @ApiOperation("获取公告")
    @GetMapping("/get")
    public ResponseModel<Announcement> getAnnouncement(@RequestParam("id")int id) {
        Announcement announcement = announcementService.getAnnouncementById(id);
        return fillSuccessData(announcement);
    }


    /**
     * 分页获取所有的公告 .
      * @param pageNum .
     * @param pageSize .
     * @return
     */
    @ApiOperation("分页获取所有的公告")
    @GetMapping("get_all")
    public ResponseModel<Page<Announcement>> getAllAnnouncement(@RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                                                                @RequestParam(value = "pageSize",defaultValue = "10")int pageSize) {
        return fillSuccessDataPage(announcementService.getAllAnnouncement(new Page<>(pageNum,pageSize)));
    }


    @ApiOperation(value = "/下载公告的附件",notes = "返回附件存在的链接及原始文件名")
    @ApiImplicitParam(name = "id",value = "Announcement表的ID",required = true,paramType = "query",dataType = "int")
    @GetMapping("/download")
    public ResponseModel<DownloadModel> downloadAttachment(@RequestParam(value = "id",required = true)int id) throws BusinessException {
        DownloadModel model = announcementService.downloadAttachment(id);
        if (model == null) {
            throw new BusinessException(PublicResultConstant.FILE_NOT_EXIST);
        }
        return fillSuccessData(model);
    }




}
